// 封装发送邮件
const nodemailer = require('nodemailer')
require('dotenv').config()

const smtpUser = process.env.SMTP_USER
const smtpTransport = nodemailer.createTransport({
  host: process.env.SMTP_HOST,
  secureConnection: true, // use SSL
  port: 465,
  secure: true,
  auth: {
    user: smtpUser,
    pass: process.env.SMTP_PASS,
  },
})

const sendMail = async (email, subject, content) => {
  if (email === '' || email === undefined) {
    return '邮箱不能为空'
  }
  const mailOptions = {
    from: smtpUser,
    to: email,
    subject,
    html: content,
  }
  try {
    const res = await smtpTransport.sendMail(mailOptions)
    if (res.response) return 'true'
    return 'false'
  } catch (err) {
    throw err
  }
}

module.exports = {
  sendMail,
}
